package com.zzy.furns.service.impl;

import com.zzy.furns.dao.FurnDAO;
import com.zzy.furns.dao.impl.FurnDAOImpl;
import com.zzy.furns.domain.Furn;
import com.zzy.furns.domain.Page;
import com.zzy.furns.service.FurnService;
import com.zzy.furns.utils.DataUtils;

import java.util.List;

public class FurnServiceImpl implements FurnService {
    private FurnDAO furnDAO = new FurnDAOImpl();
    @Override
    public List<Furn> queryFurns() {
        return furnDAO.queryFurns();
    }

    @Override
    public Boolean add(Furn furn) {
        if (furnDAO.add(furn) == 0){
            return false;
        }
        return true;
    }

    @Override
    public Boolean deleteFurnById(Integer id) {
        return furnDAO.deleteFurnById(id) == 1? true : false;
    }

    @Override
    public Furn queryFurnById(Integer id) {
        return furnDAO.queryFurnById(id);
    }

    @Override
    public Boolean updateFurn(Furn furn) {
        return furnDAO.updateFurn(furn) == 1;
    }

    @Override
    public Page<Furn> page(Integer pageNo, Integer pageSize) {
        //获取总记录数
        Integer totalRow = furnDAO.getTotalRow();
        //计算起始索引与总页数
        Integer[] arr = DataUtils.getBeginIndexAndPageTotal(totalRow, pageSize, pageNo);
        if (arr == null){
            return null;
        }
        List<Furn> furnList = furnDAO.getPageItems(arr[0], pageSize);
        return new Page<Furn>(pageNo, pageSize, totalRow, arr[1], furnList,null);
    }

    @Override
    public Page<Furn> pageByName(String name, Integer pageNo, Integer pageSize) {
        Integer pageTotalCount = furnDAO.getPageTotalCountByName(name);
        if (pageTotalCount == 0){
            return new Page<Furn>(1,pageSize,pageTotalCount,1,null,null);
        }
        Integer[] arr = DataUtils.getBeginIndexAndPageTotal(pageTotalCount, pageSize, pageNo);
        List<Furn> pageItems = furnDAO.getPageItemsByName(name, arr[0], pageSize);
        return new Page<Furn>(pageNo,pageSize,pageTotalCount,arr[1],pageItems,null);
    }
}
